package nsqlib

import (
	"github.com/nsqio/go-nsq"
	"t/log"
)

type ProServer struct{
	producer *nsq.Producer
}

func NewPro() (*ProServer, error) {
	var err error
	conf := nsq.NewConfig()
	p := new(ProServer)
	p.producer, err = nsq.NewProducer("192.168.33.11:4150", conf)
	if err != nil {
		log.Zlog.Error("nsqd 连接失败|错误信息:" + err.Error())
		return nil, err
	}

	return p, nil
}

func (p *ProServer) Publish(topic string, body string) error{
	err := p.producer.Publish(topic, []byte(body))
	if err != nil {
		log.Zlog.Error("生产失败 错误信息:" + err.Error())
		return  err
	}
	return nil
}

func (p *ProServer) Stop() {
	p.producer.Stop()
}